//
//  NSObject+StorageData.m
//  XiuXiuTuanGou
//
//  Created by 张敬 on 2018/8/16.
//  Copyright © 2018年 XiuXiuTuanGou. All rights reserved.
//

#import "NSObject+StorageData.h"

@implementation NSObject (StorageData)

- (NSString *)obtainNavPilstFileByName:(NSString *)name
{
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];
    return file;
}

- (void)writeToNavPlistFile:(id)model fileName:(NSString *)name
{
    NSString *filePath = [self obtainNavPilstFileByName:name];
    [NSKeyedArchiver archiveRootObject:model toFile:filePath];
}

- (id)readToNavPlistFileByFileName:(NSString *)name
{
    NSString *filePath = [self obtainNavPilstFileByName:name];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

- (void)removeFileByFileName:(NSString *)name
{
    NSString *filePath = [self obtainNavPilstFileByName:name];
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager isDeletableFileAtPath:filePath]) {
        [defaultManager removeItemAtPath:filePath error:nil];
    }
}

//清除缓存
- (void)clearMemoryBByController:(UIViewController *)controller callBack:(dealGeneralOperation)callBack{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定清除本地缓存？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [cancelAction setValue:UIColorFromRGB(0x363636) forKey:@"titleTextColor"];

    
    kWeakSelf(controller)
    UIAlertAction *clearAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
        [[SDImageCache sharedImageCache] clearMemory];
        [weakcontroller showHudWithMsg:@"清除缓存成功"];
        callBack();
        
    }];
    [clearAction setValue:mMainColor forKey:@"titleTextColor"];

    
    [alert addAction:cancelAction];
    [alert addAction:clearAction];
    
    [controller presentViewController:alert animated:YES completion:nil];
}


@end
